import { Body, Controller, Delete, Get, Post, Put, Query, UploadedFiles, UseInterceptors } from '@nestjs/common';
import { NewsService } from './news.service';
import { FileFieldsInterceptor } from '@nestjs/platform-express';

@Controller('news')
export class NewsController {
    constructor(
        private readonly newsService: NewsService
    ) { }

    @Post('upload')
    @UseInterceptors(FileFieldsInterceptor([{ name: 'image', maxCount: 1 }]))
    upload(@UploadedFiles() files: any, @Body() body: any) {
        return this.newsService.upload(files, body);
    }

    @Post('add')
    @UseInterceptors(FileFieldsInterceptor([{ name: 'thumbnail', maxCount: 1 }]))
    add(@UploadedFiles() files: any, @Body() body: any) {
        return this.newsService.add(files, body);
    }

    @Delete('delete')
    delete(@Body() body: any) {
        return this.newsService.delete(body.id);
    }

    @Put('update')
    @UseInterceptors(FileFieldsInterceptor([{ name: 'thumbnail', maxCount: 1 }]))
    update(@UploadedFiles() files: any, @Body() body: any) {
        return this.newsService.update(body.id, body, files);
    }

    @Get()
    index(@Query() query: any) {
        return this.newsService.findAll(query);
    }

    @Get('getNewsByCategory')
    getNewsByCategory(@Query() query: any) {
        return this.newsService.getNewsByCategory(query);
    }

    @Get('getNewsById')
    getNewsById(@Query() query: any) {
        return this.newsService.getNewsById(query);
    }
}
